package com.joy.controller;

import com.joy.command.CourseDetailCommand;
import com.joy.command.CourseQueryCommand;
import com.joy.common.UserType;
import com.joy.mapper.AssignmentMapper;
import com.joy.mapper.CourseMapper;
import com.joy.mapper.CourseStudentMapper;
import com.joy.mapper.UserMapper;
import com.joy.model.Assignment;
import com.joy.model.Course;
import com.joy.model.CourseStudent;
import com.joy.model.User;
import com.joy.vo.CourseDetailVO;
import com.joy.vo.CourseListVO;
import com.joy.vo.CourseVO;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程相关接口
 */
@Controller
public class CourseController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CourseStudentMapper courseStudentMapper;

    @Resource
    private AssignmentMapper assignmentMapper;

    @PostMapping("/queryCourses")
    @ResponseBody
    public CourseListVO queryCourses(@RequestBody CourseQueryCommand command) {
        // 传入的用户id为空，返回空数据
        if (StringUtils.isEmpty(command.getId())) {
            return new CourseListVO();
        }
        // 查询用户信息
        User user = userMapper.selectById(Long.valueOf(command.getId()));
        // 用户信息不存在，返回空数据
        if (user == null) {
            return new CourseListVO();
        }
        if (UserType.TEACHER.getType().equals(user.getType())) { // 老师查询自己的课程
            List<Course> courses = courseMapper.selectByTeacherId(user.getId());
            // 组装数据返回给前端
            List<CourseVO> courseVOS = new ArrayList<>();
            for (Course cours : courses) {
                courseVOS.add(CourseVO.from(cours));
            }
            return new CourseListVO(courseVOS);
        } else if (UserType.STUDENT.getType().equals(user.getType())) { // 学生查询自己的课程
            // 查询学生与课程的关联关系列表
            List<CourseStudent> courseStudents = courseStudentMapper.selectByStudentId(user.getId());
            if (CollectionUtils.isEmpty(courseStudents)) {
                return new CourseListVO();
            }
            // 根据课程列表id,查询课程信息
            List<Course> courses = courseMapper.selectByCourseIds(courseStudents.stream()
                    .map(item -> item.getCourseId())
                    .collect(Collectors.toList()));
            // 组装数据返回给前端
            List<CourseVO> courseVOS = new ArrayList<>();
            for (Course cours : courses) {
                courseVOS.add(CourseVO.from(cours));
            }
            return new CourseListVO(courseVOS);
        } else {
            return new CourseListVO();
        }
    }

    @PostMapping("/getCourseDetail")
    @ResponseBody
    public CourseDetailVO getCourseDetail(@RequestBody CourseDetailCommand command) {
        if (command.getId() == null) {
            return new CourseDetailVO();
        }
        Course course = courseMapper.selectById(command.getId());
        List<Assignment> assignments = assignmentMapper.selectAssignmentsByCourseId(Math.toIntExact(course.getCourseId()));


        return CourseDetailVO.from(course, assignments);
    }


}
